package cn.kgc.flightsmanagement.servcie.impl;

import cn.kgc.flightsmanagement.mapper.FlightMapper;
import cn.kgc.flightsmanagement.pojo.Flight;
import cn.kgc.flightsmanagement.pojo.FlightExample;
import cn.kgc.flightsmanagement.servcie.FlightService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author niejianhang
 * @create 2020-05-31 10:47
 */
@Service
public class FlightServiceImpl implements FlightService {

    @Resource
    FlightMapper flightMapper;

    @Override
    public PageInfo<Flight> selAll(Integer pageNum, Integer pageSize, Integer id1, Integer id2) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id");
        FlightExample flightExample=new FlightExample();
        PageInfo<Flight> pageInfo;
        if(id1!=0 && id2!=0 && id1!=id2){
            FlightExample.Criteria criteria = flightExample.createCriteria();
            criteria.andDeparturecityEqualTo(id1);
            criteria.andArrivalcityEqualTo(id2);
            List<Flight> list = flightMapper.selectByExample(flightExample);
            pageInfo=new PageInfo<>(list);
        }else{
            List<Flight> list = flightMapper.selectByExample(null);
            pageInfo=new PageInfo<>(list);
        }

        return pageInfo;
    }

    @Override
    public int del(Integer id) {
        int i = flightMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public Flight selById(Integer id) {

        Flight flight = flightMapper.selectByPrimaryKey(id);
        return flight;
    }

    @Override
    public int add(Flight flight)

    {
        int insert = flightMapper.insert(flight);
        return insert;
    }

    @Override
    public int update(Flight flight)
    {
        int i = flightMapper.updateByPrimaryKeySelective(flight);
        return i;
    }
}
